export default async function (pdfUrl) {
	const pdfDoc = await PDFJS.getDocument(pdfUrl).promise

	const totalPages = pdfDoc.numPages
	const images = []

	// debugger
	for (let i = 1; i <= totalPages; i++) {
		await pdfDoc
			.getPage(i)
			.then(async (page) => {
				// debugger
				let scaledViewport = page.getViewport({
					scale: screen.availWidth / page.getViewport({ scale: 1.0 }).width
				}) //可通过scale来调节初始的缩放比
				// debugger
				//第5步：创建一个 canvas 元素，并设置元素的画布属性
				let canvas = document.createElement('canvas')
				let context = canvas.getContext('2d')
				canvas.setAttribute('id', 'the-canvas' + i)
				context.mozImageSmoothingEnabled = false
				context.webkitImageSmoothingEnabled = false
				context.msImageSmoothingEnabled = false
				context.imageSmoothingEnabled = false
				let dpr = window.devicePixelRatio || 1
				let bsr =
					context.webkitBackingStorePixelRatio ||
					context.mozBackingStorePixelRatio ||
					context.msBackingStorePixelRatio ||
					context.oBackingStorePixelRatio ||
					context.backingStorePixelRatio ||
					1
				let ratio = dpr / bsr

				canvas.height = scaledViewport.height * ratio
				canvas.width = scaledViewport.width * ratio

				let renderContext = {
					canvasContext: context,
					viewport: scaledViewport,
					transform: [ratio, 0, 0, ratio, 0, 0] //这里会进行放大，解决模糊问题
				}
				// debugger
				//第 6 步: 使用 page.render 将数据渲染到画布上
				try {
					await page.render(renderContext).promise
					// 第 7 步: 输出图片地址
					// let image = document.createElement('img')
					// image.src = canvas.toDataURL('image/png')
					images.push(canvas.toDataURL('image/png'))
				} catch (e) {
					console.error(e)
				}
			})
			.catch(console.error)
	}

	return images
}
